package com.test.exam.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.test.exam.commons.PageHelper;
import com.test.exam.dao.ExampleDao;
import com.test.exam.dao.QuestionDao;
import com.test.exam.domain.Example;
import com.test.exam.domain.Question;

@Transactional
@Service
public class QuestionService {
	private final int QUESTION_ROW_PER_PAGE = 3; 
	@Autowired
	private QuestionDao questionDao;
	@Autowired
	private ExampleDao exampleDao;

	// end page
	public int getEndPage(int subjectIdx){
		int count = questionDao.getQuestionCountBySubjectIdx(subjectIdx);
		if(count == 0){
			return 0;
		}
		return (int)(Math.ceil((float)count/(float)QUESTION_ROW_PER_PAGE));
	}

	// 페이지 문제 목록
	public List<HashMap> getQuestionsByCurrentPage(int currentPage, int subjectIdx){
		PageHelper pageHelper = new PageHelper();
		pageHelper.setRowPerPage(QUESTION_ROW_PER_PAGE);
		pageHelper.setStartRow(currentPage);
		
		return questionDao.getQuestionsByCurrentPage(pageHelper, subjectIdx);
	}
	
	// 문제 입력
	public void addQuestion(Question question,
						String[] exampleContent){
		
		questionDao.addQuestion(question);
		int questionIdx = question.getQuestionIdx();
		
		for(int i=0; i<exampleContent.length; i++){
			Example example = new Example();
			example.setQuestionIdx(questionIdx);
			example.setExampleContent(exampleContent[i]);
			example.setExampleNo(i+1);
			exampleDao.addExample(example);
		}
	}
	
	
}
